package com.ayoka.parenting.ui.live.discussgroup;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.DiscussGroupTempRoomBean;
import com.ayoka.parenting.http.API;
import com.ayoka.parenting.http.HttpBeanInterceptCallback;
import com.ayoka.parenting.interf.CallbackUtil;
import com.ayoka.parenting.interf.RequestCallback;
import com.ayoka.parenting.mvp.BasePresenter;
import com.ayoka.parenting.ui.live.liveclassroom.LiveClassroomModel;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: DiscussGroupModel&.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class DiscussGroupModel extends LiveClassroomModel implements DiscussGroupContract.Model{

    @Override
    public void getDiscussGroupDetailData(final BasePresenter presenter, final int roomId, final String scheduleId, final RequestCallback<DiscussGroupTempRoomBean> callback) {
        API.getDiscussGroupDetailData(presenter, roomId, scheduleId, new HttpBeanInterceptCallback<BaseResponse<DiscussGroupTempRoomBean>>() {
            @Override
            public void onSuccess(BaseResponse<DiscussGroupTempRoomBean> response) {
                CallbackUtil.notifySuccess(callback, response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    getDiscussGroupDetailData(presenter, roomId, scheduleId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void updateMemberUnSpeaking(final BasePresenter presenter, final int roomId, final String userId, final RequestCallback callback) {
        API.updateMemberUnSpeaking(presenter, roomId, userId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    updateMemberUnSpeaking(presenter, roomId, userId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void updateMemberIsSpeaking(final BasePresenter presenter, final int roomId, final String userId, final RequestCallback callback) {
        API.updateMemberIsSpeaking(presenter, roomId, userId, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    updateMemberIsSpeaking(presenter, roomId, userId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void checkHasMemberIsSpeaking(final BasePresenter presenter, final int subRoomID, final RequestCallback<Integer> callback) {
        API.checkHasMemberIsSpeaking(presenter, subRoomID, new HttpBeanInterceptCallback<BaseResponse<Integer>>() {
            @Override
            public void onSuccess(BaseResponse<Integer> response) {
                CallbackUtil.notifySuccess(callback, response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    checkHasMemberIsSpeaking(presenter, subRoomID, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }
}
